package com.atguigu.tingshu.user.service.impl;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.common.rabbit.service.RabbitService;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.model.user.*;
import com.atguigu.tingshu.user.factory.StrategyFactory;
import com.atguigu.tingshu.user.mapper.*;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.strategy.ItemTypeStrategy;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.joda.time.LocalDateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private WxMaService wxMaService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitService rabbitService;

    @Autowired
    private UserPaidAlbumMapper userPaidAlbumMapper;

    @Autowired
    private UserPaidTrackMapper userPaidTrackMapper;

    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;

	@Autowired
	private VipServiceConfigMapper vipServiceConfigMapper;

	@Autowired
	private UserVipServiceMapper userVipServiceMapper;

    @Autowired
    private StrategyFactory strategyFactory;

    @Override
	@Transactional(rollbackFor = Exception.class)
    public void savePaidRecord(UserPaidRecordVo userPaidRecordVo) {
        // 根据itemType获取对应的策略对象;
        ItemTypeStrategy itemTypeStrategy = strategyFactory.getStrategy(userPaidRecordVo.getItemType());
        itemTypeStrategy.savePaidRecord(userPaidRecordVo);

        //  记录用户购买信息;
   /*     if (SystemConstant.ORDER_ITEM_TYPE_ALBUM.equals(userPaidRecordVo.getItemType())) {
            //  购买专辑 user_paid_album
            UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userPaidRecordVo.getUserId()).eq(UserPaidAlbum::getAlbumId, userPaidRecordVo.getItemIdList().get(0)));
            if (null != userPaidAlbum) {
                //  已购买
                return;
            }
            //  创建对象保存数据：
            userPaidAlbum = new UserPaidAlbum();
            //  赋值：
            userPaidAlbum.setOrderNo(userPaidRecordVo.getOrderNo());
            userPaidAlbum.setUserId(userPaidRecordVo.getUserId());
            userPaidAlbum.setAlbumId(userPaidRecordVo.getItemIdList().get(0));
            //  保存数据;
            userPaidAlbumMapper.insert(userPaidAlbum);

        } else if (SystemConstant.ORDER_ITEM_TYPE_TRACK.equals(userPaidRecordVo.getItemType())) {
            //  声音 user_paid_track; 判断当前购买的声音Id在当前表中是否存在！  单用户多平台：加锁/再查一遍
            //  用户要购买的声音列表;在订单中
            List<Long> itemIdList = userPaidRecordVo.getItemIdList();
            //  用户已购买的声音列表;
            LambdaQueryWrapper<UserPaidTrack> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserPaidTrack::getUserId, userPaidRecordVo.getUserId())
                    .in(UserPaidTrack::getTrackId, itemIdList);
            //  用户在已购买的列表中买了对应的声音：
            List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(wrapper);
            //  需要将这个集合变为set集合; 通过trackId 判断是否存在;
            Set<Long> paidTrackIdSet = userPaidTrackList.stream().map(UserPaidTrack::getTrackId).collect(Collectors.toSet());
            //  判断：
            if (!CollectionUtils.isEmpty(userPaidTrackList)) {
                //  有买过  -- 本小程序单用户单平台，此情况不会发生，相关代码没写
                for (Long itemId : itemIdList) {
                    //  判断是否包含;
                    if (paidTrackIdSet.contains(itemId)) {
                        //  需要将包含的声音添加到集合中，用来记录哪些声音属于购买过!
                        //  需要使用mq进行增加余额;mq->消息传递，要求百分百投递，重量级
                        continue;
                    }
                }
            }
            //  没有买过;
            //  根据声音Id获取声音对象;
            Result<TrackInfo> trackInfoResult = trackInfoFeignClient.getTrackInfo(userPaidRecordVo.getItemIdList().get(0));
            Assert.notNull(trackInfoResult, "查询声音信息失败");
            TrackInfo trackInfo = trackInfoResult.getData();
            Assert.notNull(trackInfo, "查询声音信息失败");

            for (Long trackId : itemIdList) {
                //  创建一个对象：
                UserPaidTrack userPaidTrack = new UserPaidTrack();
                userPaidTrack.setOrderNo(userPaidRecordVo.getOrderNo());
                userPaidTrack.setUserId(userPaidRecordVo.getUserId());
                userPaidTrack.setAlbumId(trackInfo.getAlbumId());
                userPaidTrack.setTrackId(trackId);
                //  保存数据;
                userPaidTrackMapper.insert(userPaidTrack);
            }

        } else {
            //  购买vip；user_vip_service - 可能需要续期；
			//  获取到购买时长;vip_service_config.id
            Long vipServiceId = userPaidRecordVo.getItemIdList().get(0);
			VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(vipServiceId);
			// 获取到购买的月份、时长;
			Integer serviceMonth = vipServiceConfig.getServiceMonth();
			//  保存数据：
			UserVipService userVipService = new UserVipService();
			//  赋值：
			userVipService.setOrderNo(userPaidRecordVo.getOrderNo());
			userVipService.setUserId(userPaidRecordVo.getUserId());
			Date currentTime = new Date();
			//开始生效日期
			userVipService.setStartTime(currentTime);

			//  判断当前用户是否是vip并且未过期，续期；
			UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
			if (userInfo.getIsVip() == 1 && userInfo.getVipExpireTime().after(currentTime)) {
				// 当前用户是vip且未过期 属于续期； currentTime = 过期时间
				currentTime = userInfo.getVipExpireTime();
			}
			// 获取当前时间（走了if,当前时间会变成未来的过期时间）
			LocalDateTime localDateTime = new LocalDateTime(currentTime);
			//  获取新过期时间 = 当前时间 + 购买的时长
			Date expireTime = localDateTime.plusMonths(serviceMonth).toDate();
			//  设置过期时间
			userVipService.setExpireTime(expireTime);
			//  保存vip购买记录到数据库
			userVipServiceMapper.insert(userVipService);
			//  修改用户的vip标识;
			userInfo.setIsVip(1);
			// 用户信息表有一个字段也涉及vip过期时间，需要修改
			userInfo.setVipExpireTime(expireTime);
			this.updateById(userInfo);

        }*/
    }

    @Override
    public List<Long> findUserPaidTrackList(Long userId, Long albumId) {
        // 根据用户Id 与 专辑Id 从 user_paid_track 获取到已购买的声音Id 集合列表
        List<Long> userPaidTrackIdList = userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>()
                        .eq(UserPaidTrack::getUserId, userId)
                        .eq(UserPaidTrack::getAlbumId, albumId))
                .stream().map(UserPaidTrack::getTrackId).collect(Collectors.toList());
        return userPaidTrackIdList;
    }

    @Override
    public Boolean isPaidAlbum(Long albumId, Long userId) {
        //  判断用户是否购买过专辑：user_paid_album
        Long count = userPaidAlbumMapper.selectCount(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userId).eq(UserPaidAlbum::getAlbumId, albumId));
        if (count > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Map<Long, Integer> userIsPaidTrack(Long userId, Long albumId, List<Long> trackIdList) {
        //  创建map 集合：先将所有声音的trackId -> 0
        Map<Long, Integer> map = trackIdList.stream().collect(Collectors.toMap(trackId -> trackId, trackId -> 0));
        //  判断用户是否购买过专辑：user_paid_album
        UserPaidAlbum userPaidAlbum = userPaidAlbumMapper.selectOne(new LambdaQueryWrapper<UserPaidAlbum>().eq(UserPaidAlbum::getUserId, userId).eq(UserPaidAlbum::getAlbumId, albumId));
        if (null != userPaidAlbum) {
            //  当前这个用户购买了专辑;该专辑下每一条声音的trackId -> 1
            return trackIdList.stream().collect(Collectors.toMap(trackId -> trackId, trackId -> 1));
        }
        //  没有购买专辑，查询用户是否购买过该专辑下的单集，查询购买声音的集合列表;
        List<UserPaidTrack> userPaidTrackList = userPaidTrackMapper.selectList(new LambdaQueryWrapper<UserPaidTrack>().eq(UserPaidTrack::getUserId, userId).in(UserPaidTrack::getTrackId, trackIdList));
        //  这个列表; 有可能是空：空就全0返回
        //若不为空
        if (!CollectionUtils.isEmpty(userPaidTrackList)) {
            //  购买过声音;
            for (UserPaidTrack userPaidTrack : userPaidTrackList) {
                map.put(userPaidTrack.getTrackId(), 1);
            }
        }
        //  返回数据：
        return map;

    }

    @Override
    public void updateUser(Long userId, UserInfoVo userInfoVo) {
        //  修改用户信息;
        UserInfo userInfo = new UserInfo();
        //  属性拷贝:
        BeanUtils.copyProperties(userInfoVo, userInfo);
        userInfo.setId(userId);
        this.updateById(userInfo);
    }

    @Override
    public UserInfoVo getUserInfo(Long userId) {
        // 根据id查询用户信息
        UserInfo userInfo = this.getById(userId);
        //创建对象
        UserInfoVo userInfoVo = new UserInfoVo();
        //属性拷贝
        BeanUtils.copyProperties(userInfo, userInfoVo);
        return userInfoVo;
    }

    @Override
    public Map<String, Object> wxLogin(String code) {
        //	调用微信登录api;
        WxMaJscode2SessionResult wxMaJscode2SessionResult = null;
        try {
            wxMaJscode2SessionResult = wxMaService.jsCode2SessionInfo(code);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        //	openid: 每个人的微信都对应的一个微信标识！
        String openid = wxMaJscode2SessionResult.getOpenid();
        //	利用openid 实现登录或注册功能;
        UserInfo userInfo = userInfoMapper.selectOne(new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getWxOpenId, openid));
        //	判断对象是否为空
        if (null == userInfo) {
            //	说明要注册： 本质赋值;
            userInfo = new UserInfo();
            userInfo.setWxOpenId(openid);
            userInfo.setNickname("听友:" + System.currentTimeMillis());
            userInfo.setAvatarUrl("https://oss.aliyuncs.com/aliyun_id_photo_bucket/default_handsome.jpg");
            //	保存数据到数据库;
            userInfoMapper.insert(userInfo);
            //  在此需要给当前注册的用户进行初始化充值;  同步：异步：mq 发送消息;
            rabbitService.sendMessage(MqConst.EXCHANGE_ALBUM, MqConst.ROUTING_USER_REGISTER, userInfo.getId());
        }
        //	说明要登录： 将数据存储到redis，与返回token;
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        //	组成缓存的key;
        String userLoginKey = RedisConstant.USER_LOGIN_KEY_PREFIX + token;
        //  存储数据;
        redisTemplate.opsForValue().set(userLoginKey, userInfo, RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.SECONDS);
        //  返回数据;
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        return map;
    }

}
